home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / allowo1g / domrecur.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-08-27  |  1.9 KB  |  59 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "DOMRecurse"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  16. Option Explicit
  17. ' DOMRecurse.cls    July 1999  contact markb@orionstudios.com
  18. ' Encapsulates a method to recursively traverse an HTML document from a specified
  19. '   Document Object Model (DOM) Node and raise an event for each Node encountered.
  20. ' Requires Project/References entry for
  21. '   Microsoft HTML Object Library (MSHTML.tlb)
  22. '=================================================================================
  23. Public Event NodeEvent(DOMNode As MSHTML.IHTMLDOMNode, ByVal Depth As Long)
  24.  
  25. Public Function RecurseFromNode(StartNode As MSHTML.IHTMLDOMNode) As Boolean
  26.     
  27.     RaiseEvent NodeEvent(StartNode, 0)  ' Root Node for Recursion
  28.     RecurseFromNode = ForEachNode(DOMNode:=StartNode, Depth:=1) ' Initiate Recursion
  29.     
  30. End Function
  31.  
  32. Private Function ForEachNode( _
  33.             DOMNode As MSHTML.IHTMLDOMNode, _
  34.             ByVal Depth As Long) As Boolean ' RECURSIVE
  35.  
  36.     On Error GoTo ForEachNode_Error
  37.     
  38.     Dim Result As Boolean   ' default function result = False
  39.     Dim oNode As MSHTML.IHTMLDOMNode
  40.     
  41.     Set oNode = DOMNode.firstChild
  42.     Do Until oNode Is Nothing
  43.         RaiseEvent NodeEvent(oNode, Depth + 1)
  44.         If oNode.hasChildNodes Then
  45.             ForEachNode DOMNode:=oNode, Depth:=Depth + 1 ' Recursive call
  46.         End If
  47.         Set oNode = oNode.nextSibling
  48.     Loop
  49.     Result = True
  50.     
  51. ForEachNode_Exit:
  52.     ForEachNode = Result
  53.     Exit Function
  54.  
  55. ForEachNode_Error:
  56.     Resume ForEachNode_Exit
  57.     
  58. End Function
  59.